package com.baizhi.controller;

import com.baizhi.entity.Auction;
import com.baizhi.service.AuctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class AuctionController {
    @Autowired
    private AuctionService auctionService;
    private Auction auction;
    private File pic;
    private  String picFileName;

    private int auction_id;
    private String newFileName;
    private List<Auction> auctionList;

    public AuctionService getAuctionService() {
        return auctionService;
    }

    public void setAuctionService(AuctionService auctionService) {
        this.auctionService = auctionService;
    }

    public Auction getAuction() {
        return auction;
    }

    public void setAuction(Auction auction) {
        this.auction = auction;
    }

    public File getPic() {
        return pic;
    }

    public void setPic(File pic) {
        this.pic = pic;
    }

    public String getPicFileName() {
        return picFileName;
    }

    public void setPicFileName(String picFileName) {
        this.picFileName = picFileName;
    }

    public int getAuction_id() {
        return auction_id;
    }

    public void setAuction_id(int auction_id) {
        this.auction_id = auction_id;
    }

    public String getNewFileName() {
        return newFileName;
    }

    public void setNewFileName(String newFileName) {
        this.newFileName = newFileName;
    }

    public List<Auction> getAuctionList() {
        return auctionList;
    }

    public void setAuctionList(List<Auction> auctionList) {
        this.auctionList = auctionList;
    }

    @RequestMapping("/addAuction")
    public String addAuction(MultipartFile mf, Auction auction, HttpSession session)throws IOException {
        //把文件赋值到tomcat里面，1，动态获取tomcat路径 2，获取文件的后缀名 3，生成一个新的文件名
        String path = session.getServletContext().getRealPath("/page/images");
        //判断这个路径是否存在，如果不存在就创建出这个目录
        System.out.println(path);
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        //获取老的文件名
        String oldName = mf.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        //相对路径。数据库表里面的path存储的是这个路径
        String relPath = "/page/images" + newName;
        //绝对路径，jar包上传的具体位置
        String adcPath = session.getServletContext().getRealPath(relPath);
        mf.transferTo(new File(adcPath));
        //把jar对象添加到数据库里面
        auction.setAuction_pic(oldName);

        System.out.println();
        auctionService.addAuction(auction);
        System.out.println(auction);
        return "redirect:/selectAllAuction";
    }

    @RequestMapping("/selectAllAuction")
    public String selectAllAuction(Map map){
       List<Auction> auctionList= auctionService.selectAllAuction();
       map.put("auctionList",auctionList);
        System.out.println(auction);
        return "page/paimaiguanliye";
    }
    @RequestMapping("/delete")
    public String delete(int id){
        auctionService.delete(id);
        return "redirect:selectAllAuction";
    }
}
